var util = require('../../utils/util.js');
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      {
        link: '/pages/index/index',
        url: 'https://tiaonali.com/zhiwei31.jpg'
      }, {
        link: '/pages/news/index',
        url: 'https://tiaonali.com/zhiwei3.jpg'
      }
    ],
    getHunterList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    userInfo: {},
    pageNum: 1,
    isFirstLoad: true,
    noMore: false,
    curr_page_no: 1,
    totalPages: 0,
    banner: {},
    labels:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    util.post(app.api.getAdcarouse, { type: 2 }, function (res) {
      //获取首页轮播图
      that.setData({
        banner: res.data
      });
      console.log(that.data.banner);
    });
    this.resetData(true);
  },

  resetData: function (isRefresh) {
    var that = this;
    var pageNo = 1;
    if (!isRefresh) {
      //加载更多
      pageNo = that.data.curr_page_no + 1;
    }
    util.post(app.api.getHunterList, { page: pageNo, size: 10 }, function (res) {
      if (isRefresh) {
        //执行刷新 当前页号重置为1
        that.setData({
          curr_page_no: 1,
        });
        if (res == null && res.data == null && res.data.data.length < 0) {
          //刷新，没有数据
          wx.showToast({
            title: '没有数据',
            icon: 'loading',
            duration: 1200
          })
          that.setData({
            articleInfo: null,
            totalPages: 0,
            noMore: true
          });
        } else {
          //刷新，有数据
          that.setData({
            getHunterList: res.data.data,
            totalPages: res.data.pageNum,
            noMore: false
          });
        }
      }else{
        //加载更多
        console.log("更多-----》");
        that.setData({
          curr_page_no: pageNo
        });
      
      
      if (res == null && res.data == null && res.data.data.length < 0) {
        //更多，没有数据
        console.log("更多-----数据为空》");
        wx.showToast({
          title: '没有更多数据',
          icon: 'loading',
          duration: 1200
        })
        that.setData({
          noMore: true
        });
      } else {
        wx.showToast({
          title: '加载更多',
          icon: 'loading',
          duration: 600
        })
        console.log("更多-----数据不为空》");
        console.log(res);
        var dtList = res.data.data;
        console.log(that.data.getHunterList);
        var result = that.data.getHunterList;
        for (var j = 0; j < dtList.length; j++) {
          result.push(dtList[j]);
        }
        var tmpNo = that.data.pageNo;
        tmpNo = tmpNo + 1;
        //更多，有数据
        that.setData({
          pageNo: tmpNo,
          getHunterList: result,
          totalPages: res.data.pageNum,
          noMore: false
        });
        console.log(that.data.getHunterList);
      }
    }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onShow");
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("onHide");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    // 显示导航栏loading  
    wx.showNavigationBarLoading();
    // 调用接口加载数据  
    this.resetData(true);
    // 隐藏导航栏loading  
    wx.hideNavigationBarLoading();
    // 当处理完数据刷新后，wx.stopPullDownRefresh可以停止当前页面的下拉刷新  
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.curr_page_no >= that.data.totalPages) {
      console.log("onReachBottom noMore ");
      //已经是最后一页
      that.setData({
        noMore: true,
      });
    } else {
      this.resetData(false);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //猎头详细信息
  hunterTap: function (event) {
    var id = event.currentTarget.dataset.id; // 当前id
    console.log("猎头详情id获取=" + id)
    // 切换页面
    wx.navigateTo({
      url: '../hunter-detail/hunter-detail?hunterid=' + id
    });
  }
})